package com.citywithincity.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class EntityHelper {
	public static <T> List<T> getList(Connection connection,Entity<T> entityInfo,String sql,List<Object> whereValues) throws SQLException{
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = connection.prepareStatement(sql);
			setPs(whereValues,ps);
			rs = ps.executeQuery();
			return entityInfo.getList(rs);
		} finally {
			DbUtil.close(rs, ps);
		}
	}
	
	
	
	
	
	protected static int setPs(List<Object> whereValues,PreparedStatement ps) throws SQLException {
		if (whereValues.size() > 0) {
			int index = 1;
			for (Object object : whereValues) {
				ps.setObject(index++, object);
			}
			return index;
		}
		return 1;
	}
}
